<template>
<view>
<view class="container" v-if="applyMendianType">
  <view class="tongji_container" :style="'background:' + setting.platformSetting.defaultColor" data-link="tunzhu_account.html" @tap="tolinkUrl">      
    <view class="tongji_mid font_24">{{mendian.account.account}}</view>
    <view class="tongji_top">账户{{properties.alias_yue||'余额'}}(￥)</view>
  </view>
  <view class="tongjiContainer_form">
      <view class="tongji_bottom">
        <view data-link="mendian_zijin.html" @tap="tolinkUrl" class="tongji_bottom_item">
          <view :style="'color:' + setting.platformSetting.defaultColor + ';'">
            <text>￥</text>
            <text>{{mendian.totalEarningAmount}}</text>
          </view> 
          <text>总收入</text>
           <view class="borderRight2"></view>
        </view>
        <view data-link="mendian_tixian.html" @tap="tolinkUrl" class="tongji_bottom_item">
          <view :style="'color:' + setting.platformSetting.defaultColor + ';'">
            <text>￥</text>
            <text>{{mendian.totalTixianAmount}}</text>
          </view>
          <text>总提现</text>
           <view class="borderRight2"></view>
        </view>
        <view data-link="mendian_will_order.html" @tap="tolinkUrl" class="tongji_bottom_item">
          <view :style="'color:' + setting.platformSetting.defaultColor + ';'">
            <text>￥</text>
            <text>{{mendian.waitCompleteOrderDistributeAmount}}</text>
          </view>
          <text>待实现</text>
        </view>
      </view>

    <view class="mendian_menu_container">
        <view class="mendian_menu_item" data-link="mendian_zijin.html" @tap="tolinkUrl">
            <view class="imageContainer">
                <image src="https://image1.sansancloud.com/xianhua/2018_9/5/16/19/25_19.jpg?x-oss-process=style/preview_120"></image>
            </view>
            <view class="item_text">
                <text>资金记录</text>
            </view>
            <view class="borderRight"></view>
        </view>
        <view class="mendian_menu_item" data-link="mendian_order.html" @tap="tolinkUrl">
            <view class="imageContainer">
                <image src="https://image1.sansancloud.com/xianhua/2018_9/5/16/19/25_14.jpg?x-oss-process=style/preview_120"></image>
            </view>
            <view class="item_text">
                <text>订单列表</text>
            </view>
            <view class="borderRight"></view>
        </view>
        <!-- <view class='mendian_menu_item' data-link='mendian_user.html?fxLevel=1'   bindtap='tolinkUrl'>
            <view class='imageContainer'>
                <image src='https://image1.sansancloud.com/xianhua/2018_9/5/16/19/25_53.jpg?x-oss-process=style/preview_120'></image>
            </view>
            <view class='item_text'>
                <text>我的客户</text>
            </view>
        </view> -->
        
        <!-- <view class='mendian_menu_item' data-link='mendian_back_item.html'   bindtap='tolinkUrl'>
            <view class='imageContainer'>
                <image src='http://image.aikucun.xyz/aikucun/2018_4/2/14/11/22_815.jpg?imageMogr2/thumbnail/400x/interlace/0 '></image>
            </view>
            <view class='item_text'>
                <text>售后服务</text>
            </view>
            <view class='borderRight'></view>
             <view class='red_poient_num' wx:if='{{mendian.waitHandleBackItem}}'>{{mendian.waitHandleBackItem}}</view>
        </view> -->
        <view class="mendian_menu_item" data-link="mendian_will_order.html" @tap="tolinkUrl">
            <view class="imageContainer">
                <image src="https://image1.sansancloud.com/xianhua/2018_9/5/16/19/25_55.jpg?x-oss-process=style/preview_120"></image>
            </view>
            <view class="item_text">
                <text>待实现收入</text>
            </view>
            <view class="borderRight"></view>
        </view>
        <!-- <view class='mendian_menu_item' data-link='news_list.html?newsTypeId=6&pageNage=帮助中心'   bindtap='tolinkUrl'>
            <view class='imageContainer'>
                <image src='https://image1.sansancloud.com/xianhua/2018_9/5/16/19/25_64.jpg?x-oss-process=style/preview_120'></image>
            </view>
            <view class='item_text'>
                <text>帮助</text>
            </view>
        </view> -->
        <view class="mendian_menu_item" data-link="mendian_qrcode.html" @tap="tolinkUrl">
            <view class="imageContainer">
                <image src="https://image1.sansancloud.com/xianhua/2018_9/5/16/19/25_60.jpg?x-oss-process=style/preview_120"></image>
            </view>
            <view class="item_text">
                <text>二维码</text>
            </view>
            <view class="borderRight"></view>
        </view>
        <view class="mendian_menu_item" @tap="getVerificationCode">
            <view class="imageContainer">
                <image src="https://image1.sansancloud.com/xianhua/2019_12/18/22/5/32_484.jpg?x-oss-process=style/preview_120"></image>
            </view>
            <view class="item_text">
                <text>扫一扫</text>
            </view>
            <view class="borderRight"></view>
        </view>   
        <view class="mendian_menu_item" data-link="mendian_management_product.html" @tap="tolinkUrl">
            <view class="imageContainer">
                <image src="https://image1.sansancloud.com/xianhua/2019_8/1/10/35/44_598.jpg?x-oss-process=style/preview_120"></image>
            </view>
            <view class="item_text">
                <text>商品管理</text>
            </view>
            <view class="borderRight"></view>
        </view>       
        <!-- <view class='mendian_menu_item'>
            <form bindsubmit="submitFormId" report-submit='true' >
              <button form-type="submit" style='background:none !important; border:none !important;'></button>
            </form>
            <view class='imageContainer'>
                <image src='https://image1.sansancloud.com/xianhua/2020_3/14/17/48/59_177.jpg?x-oss-process=style/preview_120'></image>
            </view>
            <view class='item_text'>
                <text>申请服务商</text>
            </view>
            <view class='borderRight'></view>
        </view> -->
            
                  
    </view>
  </view>
</view>
<view class="noMendian" v-else>
  <image mode="aspectFit" class="img" src="https://image1.sansancloud.com/xianhua/2019_5/13/17/2/38_813.jpg?x-oss-process=style/preview_120"></image>
  <text>主人~!您还没有申请入住过哦~</text>
</view>
</view>
</template>

<script>

export default {
  data() {
    return {
      setting: {},
      loginUser: null,
      mendian: {
        account: {
          account: 0
        },
        totalEarningAmount: 0.00,
        totalTixianAmount: 0.00,
        waitCompleteOrderDistributeAmount: 0.00
      },
      account: {
        account: 0
      },
      applyMendianType: true,
      properties: ""
    };
  },

  components: {},
  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    console.log('======app.loginUser======', getApp().globalData.loginUser);
    console.log('====== app.setting======', getApp().globalData.setting);

    if (getApp().globalData.loginUser) {
      this.checkState();
    } else {
      getApp().globalData.addLoginListener(this); // wx.showLoading({
      //   title: 'loading'
      // })

      getApp().globalData.showToastLoading('loading', true);
      console.log("====setTimeout1=====");
      that.setTimeoutLogin(that.conut);
    }

    this.setData({
      setting: getApp().globalData.setting,
      loginUser: getApp().globalData.loginUser,
      properties: getApp().globalData.properties
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onLoad();
    setTimeout(function () {
      wx.stopPullDownRefresh();
    }, 2000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {}
  /**
   * 用户点击右上角分享
   */
  ,
  methods: {
    /* 组件事件集合 */
    tolinkUrl: function (e) {
      let linkUrl = e.currentTarget ? e.currentTarget.dataset.link : e;
      getApp().globalData.linkEvent(linkUrl);
    },
    submitFormId: function (e) {
      this.toApplyForFacilitator(e);
    },
    getMendianInfo: function () {
      console.log('-------门店-1-------');
      let params = {};
      var customIndex = getApp().globalData.AddClientUrl("/ge_manager_mendian_info_admin_mendian_json.html", params, 'post');
      var that = this;
      getApp().globalData.showToastLoading('loading', true);
      wx.request({
        url: customIndex.url,
        data: customIndex.params,
        header: getApp().globalData.headerPost,
        method: 'POST',
        success: function (res) {
          console.log(res.data);

          if (res.data.errcode == '0') {
            let mendian = res.data.relateObj;
            mendian = that.dellMoney(mendian); //account 账户余额

            that.setData({
              mendian: mendian
            });
            that.setNav(mendian);
          } else {
            wx.showModal({
              title: '失败了',
              content: '请求失败了，请下拉刷新！'
            });
          }
        }
      });
    },
    // getMendianInfo: function () {
    //   console.log('-------门店-1-------');
    //   let params = {};
    //   var customIndex = getApp().globalData.AddClientUrl("/ge_manager_mendian_info_admin_mendian_json.html", params, 'post');
    //   var that = this; // wx.showLoading({
    //   //     title: 'loading'
    //   // })

    //   getApp().globalData.showToastLoading('loading', true);
    //   wx.request({
    //     url: customIndex.url,
    //     data: customIndex.params,
    //     header: getApp().globalData.headerPost,
    //     method: 'POST',
    //     success: function (res) {
    //       console.log(res.data);

    //       if (res.data.errcode == '0') {
    //         let mendian = res.data.relateObj;
    //         mendian = that.dellMoney(mendian); //account 账户余额

    //         that.setData({
    //           mendian: mendian
    //         });
    //         that.setNav(mendian);
    //       } else {
    //         wx.showModal({
    //           title: '失败了',
    //           content: res.data.errMsg
    //         });
    //       }

    //       wx.hideLoading();
    //     },
    //     fail: function (res) {
    //       wx.showModal({
    //         title: '失败了',
    //         content: '请求失败了，请下拉刷新！'
    //       });
    //       wx.hideLoading();
    //       getApp().globalData.loadFail();
    //     }
    //   });
    // },
    dellMoney: function (mendian) {
      mendian.account.account = getApp().globalData.toFix(mendian.account.account);
      mendian.totalEarningAmount = getApp().globalData.toFix(mendian.totalEarningAmount);
      mendian.totalTixianAmount = getApp().globalData.toFix(mendian.totalTixianAmount);
      mendian.waitCompleteOrderDistributeAmount = getApp().globalData.toFix(mendian.waitCompleteOrderDistributeAmount);
      return mendian;
    },
    setNavColor: function () {
      console.log('setNavColor', getApp().globalData.setting);
      wx.setNavigationBarColor({
        frontColor: '#ffffff',
        backgroundColor: getApp().globalData.setting.platformSetting.defaultColor
      });
    },
    setNav: function (mendian) {
      console.log('setNav', getApp().globalData.setting);
      wx.setNavigationBarTitle({
        title: mendian.name
      });
    },
    //扫一扫 登录后台
    toLoginBackstage: function (e) {
      console.log("toLoginBackstage", e);
      wx.scanCode({
        onlyFromCamera: true,
        success: scanRes => {
          let params = {};
          var customIndex = getApp().globalData.AddClientUrl("/wx_mini_scan_uuid.html", params, 'post');

          if (scanRes.errMsg == 'scanCode:ok') {
            let {
              result
            } = scanRes; // 跳转-确认、取消登录页面

            wx.navigateTo({
              url: '/pages/scan_login/scan_login?scan_result=' + result,

              success(data) {
                wx.request({
                  url: customIndex.url,
                  data: customIndex.params,
                  header: getApp().globalData.headerPost,
                  method: 'POST',
                  success: function (res) {
                    console.log('扫码成功');
                  },
                  fail: function (res) {}
                });
              }

            });
          }
        }
      });
    },
    //扫一扫 核销
    getVerificationCode: function (e) {
      console.log("getVerificationCode", e);
      wx.scanCode({
        onlyFromCamera: true,
        success: scanRes => {
          console.log("getVerificationCode", scanRes);
          wx.navigateTo({
            url: "/" + scanRes.path
          });
        }
      });
    },
    //跳转到申请服务商web页面
    toApplyForFacilitator: function (e) {
      wx.navigateTo({
        url: '/pages/apply_for_facilitator/apply_for_facilitator?formId=' + e.detail.formId
      });
    },
    loginSuccess: function (user) {
      console.log("pre apply mendian login success call back!", user);
      this.checkState();
    },
    loginFailed: function (err) {
      console.log("login failed!!");
    },
    checkState: function () {
      console.log('======checkState.loginUser======', getApp().globalData.loginUser);
      this.setData({
        setting: getApp().globalData.setting,
        loginUser: getApp().globalData.loginUser
      });
      this.setNavColor();
      this.getMendianInfo();

      if (!getApp().globalData.loginUser.platformUser.managerMendianId) {
        console.log("=========没有管理店铺=========");
        this.setData({
          applyMendianType: false
        });
        return;
      } else {}
    },
    setTimeoutLogin: function (conuData) {
      let that = this;
      console.log("====setTimeout-init=====", conuData);
      that.conut = conuData;
      that.conut += 2;

      if (that.conut <= 5) {
        setTimeout(function () {
          if (getApp().globalData.loginUser) {
            wx.hideLoading();
          } else {
            that.setTimeoutLogin(that.conut);
          }
        }, that.conut * 1000);
      } else {
        wx.showModal({
          title: '失败了',
          content: '请求失败了，请下拉刷新！'
        });
      }
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
@import "./index.css";
</style>